package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.CarSeriesModelAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarSeriesModelBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/3/2.
 * 车系
 */

public class CarSeriesActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<CarSeriesModelBean> data;
    private CarSeriesModelAdapter adapter;
    private String brandId;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_series_model);
        ButterKnife.inject(this);
        brandId = getIntent().getStringExtra("brandId");
        getCarSeries();
        type = getIntent().getIntExtra("Brand", 1);
    }

    private void getCarSeries() {
        HashMap<String, Object> build = new MapUtil()
                .put("brand_id", brandId)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).getCarSeries()
                , new BaseSubscriber<TokenBean<List<CarSeriesModelBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<CarSeriesModelBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            initViews(o.getData());
                        }

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });

    }

    private void initViews(List<CarSeriesModelBean> data) {
        this.data = data;
        adapter = new CarSeriesModelAdapter(CarSeriesActivity.this, R.layout.layout_item_select_car_brand, data);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }


    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        for (int i = 0; i < data.size(); i++) {
            if (i == position) {
                data.get(i).setCheck(true);
            } else {
                data.get(i).setCheck(false);
            }
        }
        if (type == 1) {
            Intent intent = new Intent(CarSeriesActivity.this, CarModelActivity.class);
            intent.putExtra("seriesId", data.get(position).getId());
            BasicInfoActivity.carSeriesId = data.get(position).getId();
            BasicInfoActivity.carSeriesName = data.get(position).getName();
            Logger.d("carSeriesId===" + data.get(position).getId() + "  " + data.get(position).getName());
            startIntent(intent, RequestAndResultCode.CarSeriesAcitivty_TO_CarModelActivity);

        } else {
            Intent intent = new Intent();
            BasicInfoActivity.carSeriesId = data.get(position).getId();
            BasicInfoActivity.carSeriesName = data.get(position).getName();
            intent.putExtra("seriesId",data.get(position).getId());
            intent.putExtra("seriesName",data.get(position).getName());
            setResult(RequestAndResultCode.CarSeriesActivity_Result, intent);
            finish();
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RequestAndResultCode.CarModelActivity_Result) {
            setResult(RequestAndResultCode.CarSeriesActivity_Result);
            finish();
        }
    }
}
